package com.clstu.generic;

import java.util.ArrayList;

public class Generic01 {
    public static void main(String[] args) {
        ArrayList<Dog> arrayList = new ArrayList<Dog>();
        arrayList.add(new Dog("旺财",10));
        arrayList.add(new Dog("招财",10));
        arrayList.add(new Dog("大黄",10));
        arrayList.add(new Dog("汤姆",10));
        //arrayList.add(new Cat("旺财",10));//用了泛型这里就限制了只能加入Dog对象，加入Cat对象则会报错
        for (Dog dog:arrayList) {//不用泛型这里就只能默认接受Object对象，用了泛型就可以直接用Dog接收，省去向下转型
           // Dog dog=(Dog)o;//不用泛型这里会有转型的问题,比如遇到一只cat
            System.out.println(dog.getName()+"----"+dog.getAge());
        }

    }
}
class Dog{
    private String name;
    private int age;

    public Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Cat{
    private String name;
    private int age;

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}